import createAxiosClient from "@/app/utils/axiosRequest";
import {response} from "express";
import {decodeFromBase64} from "next/dist/build/webpack/loaders/utils";
import cookie from "react-cookies";
import {UserName, Token} from "@/app/constants/authTokenConstants";
import {toast} from "sonner";

// 参数接口定义
export interface CreateShareParams {
  /*分享笔记ID */
  noteId: number;

  /*权限类型 READ-只读 WRITE-可编辑 */
  permission?: string;

  /*过期时间 */
  expiredAt?: Date | string;
}

// 分享项接口
export interface ShareItem {
  id: number;
  noteId: number;
  userId: number;
  noteTitle?: string;
  token: string;
  permission: string;
  expiredAt: string;
  createdAt: string;
  updatedAt: string;
}

// 分页响应接口
export interface PageResponse<T> {
  records: T[];
  total: number;
  size: number;
  current: number;
}

/**
 * 创建分享
 * @param params 创建分享请求参数
 */
export async function createShare(params: CreateShareParams) {
  try {
    const axiosInstance = createAxiosClient();
    
    // 创建参数的副本
    const requestData = {...params};
    
    // 如果提供了expiredAt且是字符串格式，确保使用ISO格式（将空格替换为T）
    if (requestData.expiredAt && typeof requestData.expiredAt === 'string') {
      requestData.expiredAt = requestData.expiredAt.replace(' ', 'T');
    }
    
    const response = await axiosInstance.post(`/app/api/v1/shares`, requestData);
    toast.success("创建分享成功");
    return response.data;
  } catch (error: any) {
    toast.error(error.message || "创建分享失败");
    throw error;
  }
}

/**
 * 取消分享
 * @param shareId 分享ID
 */
export async function cancelShare(shareId: number) {
  try {
    const axiosInstance = createAxiosClient();
    const response = await axiosInstance.delete(`/app/api/v1/shares/${shareId}`);
    toast.success("取消分享成功");
    return response.data;
  } catch (error: any) {
    toast.error(error.message || "取消分享失败");
    throw error;
  }
}

/**
 * 获取用户分享
 * @param current 当前页码
 * @param size 每页条数
 */
export async function getUserShares(current: number = 1, size: number = 10): Promise<any> {
  try {
    console.log("getUserShares - 开始请求，参数:", { current, size });
    const axiosInstance = createAxiosClient();
    const response = await axiosInstance.get(`/app/api/v1/shares/user?current=${current}&size=${size}`);
    console.log("getUserShares - 收到原始响应:", response);
    
    // 返回完整响应，不要过滤
    return response.data;
  } catch (error: any) {
    console.error("getUserShares - 请求失败:", error);
    toast.error(error.message || "获取分享列表失败");
    throw error;
  }
}

/**
 * 获取笔记分享
 * @param noteId 笔记ID
 * @param current 当前页码
 * @param size 每页条数
 */
export async function getNoteShares(noteId: number, current: number = 1, size: number = 10): Promise<any> {
  try {
    console.log("getNoteShares - 开始请求，参数:", { noteId, current, size });
    const axiosInstance = createAxiosClient();
    // 请求API
    const response = await axiosInstance.get(`/app/api/v1/shares/note/${noteId}?current=${current}&size=${size}`);
    console.log("getNoteShares - 收到原始响应:", response);
    
    // 返回完整响应，不要过滤
    return response.data; 
  } catch (error: any) {
    console.error("getNoteShares - 请求失败:", error);
    toast.error(error.message || "获取笔记分享失败");
    throw error;
  }
}

/**
 * 获取分享链接
 * @param token 分享令牌
 */
export function getShareLink(token: string): string {
  return `${window.location.origin}/share/${token}`;
} 